﻿using DMSkin.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Dt261.Model;
using Dt261.ViewModel;
using System.Collections.ObjectModel;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;
using System.Threading;
using System.Windows.Threading;
using Dt261.jsonmodel;

namespace Dt261.Views
{
    /// <summary>
    /// UserPage.xaml 的交互逻辑
    /// </summary>
    public partial class UserPage : Page
    {
        //private Collection<string> hzs = new Collection<string> { "125", "250", "500", "1K", "2K", "4K", "8K" };
        //private Collection<int> hzsint = new Collection<int> { 125, 250, 500, 1000, 2000, 4000, 8000 };

        private Collection<string> hzs = new Collection<string> { "125", "250", "500", "1K", "2K", "4K", "8K" };
        private Collection<int> hzsint = new Collection<int> { 125, 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 6000, 8000 };

        Dictionary<int, double> hzsPxValue = new Dictionary<int, double> { { 125, 0 }, { 250, 1 }, { 500, 2 },{ 750, 2.5 }, { 1000, 3 }, { 1500, 3.5 }, { 2000, 4 },
            { 3000, 4.5 }, { 4000, 5 },{ 6000, 5.5 }, { 8000, 6 } };

        //Dictionary<int, int> limitHz = new Dictionary<int, int> { { 125, 65 }, { 250, 85 }, { 500, 105 }, { 750, 105 },{ 1000, 105 }, { 1500, 105 }, { 2000, 105 },
        //    { 3000, 105 }, { 4000, 105 },{ 6000, 105 }, { 8000, 95 } };
        private int topM = 45;
        private int rightM = 80;
        private double px;
        private double py;

        private List<Point> dataPoints = new List<Point>();
        private PointCollection coordinatePoints = new PointCollection();
        private List<Shape> pointEllipses = new List<Shape>();
        private List<Shape> pointEllipses2 = new List<Shape>();

        private List<Shape> pointBcEllipses = new List<Shape>();//右耳骨导的电线
        private List<Shape> pointBcEllipses2 = new List<Shape>();//左耳骨导的电线
        Polyline curvePolyline = new Polyline();


        private Point yuandian = new Point(40, 305);

        public UserPage()
        {
            InitializeComponent();

            curvePolyline.Stroke = Brushes.Blue;
            curvePolyline.StrokeThickness = 2;

            px = (this.chartCanvas.Width - yuandian.X - rightM) / 6.0;
            py = (yuandian.Y - topM) / 13.0;

            DrawScale(this.chartCanvas, Color.FromRgb(0xd9, 0x4f, 0x54));
            DrawPoint(this.chartCanvas, pointEllipses);
            DrawCurve(this.chartCanvas);

            DrawScale(this.chartCanvas2, Color.FromRgb(0x55, 0xa4, 0xdd));

            UserPageViewModel model = new UserPageViewModel();
            this.DataContext = model;
            model.SelectPureToneResultEvent += UpdateDataSets;

            model.SelectMemberChangeEvent += UpdateSelectMemberItem;

            
            if (GlobalData.LoginNetUser != null)
            {
                model.getInitInfoByNetUser();
            }
            else if(GlobalData.Host != null && GlobalData.Host != "")
            {
                model.getInitInfoByPostHost();
            }
            else
                model.getInitInfo();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            UserPageViewModel model = (UserPageViewModel)this.DataContext;
            // Broadcast.PushBroadcast("Navigation", new Page1());
            if (model.SelectMember == null)
            {
                var result = MessageBoxX.Show("请先添加测试成员", "提醒", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                {
                    MessageBoxStyle = MessageBoxStyle.Modern,
                    MessageBoxIcon = MessageBoxIcon.Warning,
                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                });

                //if (result == MessageBoxResult.Yes)
                //{

                //    DMSkin.Core.Broadcast.PushBroadcast("Navigation", new UserInfoPage(model.SelectMember));
                //    // DialogService.ShowMessage("Delete friend successfully");
                //}
            }
            else
            {
                new TestWindow(model.SelectMember).ShowDialog();
            }



        }

        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            new TestWindow().ShowDialog();
        }

        private void UpdateSelectMemberItem()
        {
            Console.WriteLine("UpdateSelectMemberItem  start");
            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new
                    DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                SynchronizationContext.Current.Post(pl =>
                {
                    this.chartCanvas.Children.Clear();
                    this.chartCanvas2.Children.Clear();
                    DrawScale(this.chartCanvas, Color.FromRgb(0xd9, 0x4f, 0x54));
                    DrawScale(this.chartCanvas2, Color.FromRgb(0x55, 0xa4, 0xdd));

                    UserPageViewModel model = (UserPageViewModel)this.DataContext;
                    //Task.Run(() =>
                    model.SelectResult();
                    //);
                    Console.WriteLine("UpdateSelectMemberItem  end");
                }, null);
            });

        }


        private void UpdateDataSets(List<ResultDataSet> DataSets, int bcneedlink = 0)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new
                    DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                SynchronizationContext.Current.Post(pl =>
                {

                    this.chartCanvas2.Children.Clear();
                    this.chartCanvas.Children.Clear();
                    {
                        // this.chartCanvas.Children.Clear();
                        DrawScale(this.chartCanvas, Color.FromRgb(0xd9, 0x4f, 0x54));
                    }

                    {
                        //  this.chartCanvas2.Children.Clear();

                        DrawScale(this.chartCanvas2, Color.FromRgb(0x55, 0xa4, 0xdd));
                        //foreach (Shape dataEllipse in pointEllipses2)
                        //{
                        //    this.chartCanvas2.Children.Remove(dataEllipse);
                        //}
                    }
                    foreach (ResultDataSet dataset in DataSets)
                    {
                        Dictionary<int, TestItem> dict = new Dictionary<int, TestItem>();
                        foreach (ResultEntry entry in dataset.Entrys)
                        {
                            TestItem item = new TestItem();
                            item.Hz = entry.ValueX;
                            item.Db = entry.ValueY;
                            if (entry.PointState == 1)
                            {
                                item.Mask = true;
                                item.MaskV = 0;
                                //...
                            }
                            else
                            {
                                item.Mask = false;
                                item.MaskV = 0;
                                //...
                            }

                            if (entry.IsUserResp == 0)
                                item.IsUserResp = false;
                            else
                                item.IsUserResp = true;
                            item.abc = entry.Mmtype;
                            item.MarkType = entry.MarkType;
                            dict.Add(item.Hz, item);

                            //List<TestItem> listChild = this.rightResult.ToList();
                            //listChild.Sort(delegate (TestItem p1, TestItem p2) { return Comparer<int>.Default.Compare(p1.Hz, p2.Hz); });
                            ////List列表转为ObservableCollection集合
                            //this.rightResult = new ObservableCollection<TestItem>(listChild);
                        }
                        //this.IsRightEar 
                        UpdateTestItem(dict, dataset.IsRight == 1, dataset.HeadsetMode, bcneedlink);
                    }

                    Console.WriteLine("chartCanvas2.Children.Count = " + chartCanvas2.Children.Count);

                    Console.WriteLine("chartCanvas.Children.Count = " + chartCanvas.Children.Count);
                    //DispatcherFrame frame = new DispatcherFrame();
                    //Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrames), frame);
                    //try { Dispatcher.PushFrame(frame); }
                    //catch (InvalidOperationException) { }

                }, null);
            });




        }


        private static object ExitFrames(object frame)
        {
            ((DispatcherFrame)frame).Continue = false;
            return null;

        }
        private void UpdateTestItem(Dictionary<int, TestItem> list, bool isRight, int mmtype = 0, int bcneedlink = 0)
        {
            Console.WriteLine("UpdateTestItem  start");



            //  this.Dispatcher.Invoke(DispatcherPriority.Normal,
            //(ThreadStart)delegate () {








            if (mmtype == 0)
            {
                if (isRight)
                {
                    //foreach (Shape dataEllipse in pointEllipses)
                    //{
                    //    this.chartCanvas.Children.Remove(dataEllipse);
                    //}

                    if (list == null) return;

                    TestItem lastitem = null;
                    for (int i = 0; i < this.hzsint.Count(); i++)
                    {
                        //(40,280)原点坐标,上面空45，右边空80
                        // this.chartCanvas.Children.Clear();
                        if (list.ContainsKey(this.hzsint[i]))
                        {
                            TestItem item = list[this.hzsint[i]];
                            if (item.Mask == true)
                            {
                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                    topM + (10 + item.Db) / 10.0 * py,
                                    item.MarkType != 0 ? item.MarkType : 10,
                                    true,
                                    this.chartCanvas,
                                    pointEllipses);

                            }
                            else
                            {


                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                         topM + (10 + item.Db) / 10.0 * py,
                         item.MarkType != 0 ? item.MarkType : 1,
                         true,
                         this.chartCanvas,
                         pointEllipses);
                            }



                            if (item.IsUserResp != true)
                            {
                                TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                   topM + (10 + item.Db) / 10.0 * py,
                                   isRight,
                                    this.chartCanvas,
                                   pointEllipses);

                            }


                            if (lastitem != null)
                            {



                                Line line = new Line();
                                line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

                                line.Y1 = topM + (10 + lastitem.Db) / 10.0 * py;
                                line.X1 = yuandian.X + hzsPxValue[lastitem.Hz] * px;
                                line.Y2 = topM + (10 + item.Db) / 10.0 * py;
                                line.X2 = yuandian.X + hzsPxValue[item.Hz] * px;
                                line.StrokeThickness = 1;


                                this.chartCanvas.Children.Add(line);
                                pointEllipses.Add(line);

                            }


                            lastitem = item;



                        }

                    }

                }
                else
                {
                    //foreach (Shape dataEllipse in pointEllipses2)
                    //{
                    //    this.chartCanvas2.Children.Remove(dataEllipse);
                    //}

                    if (list == null) return;
                    TestItem lastitem = null;
                    for (int i = 0; i < this.hzsint.Count(); i++)
                    {
                        //(40,280)原点坐标,上面空45，右边空80
                        // this.chartCanvas.Children.Clear();
                        if (list.ContainsKey(this.hzsint[i]))
                        {
                            TestItem item = list[this.hzsint[i]];

                            if (item.IsUserResp != true)
                            {
                                TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                   topM + (10 + item.Db) / 10.0 * py,
                                   isRight,
                                    this.chartCanvas2,
                                   pointEllipses2);

                            }


                            if (item.Mask == true)
                            {


                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                         topM + (10 + item.Db) / 10.0 * py,
                         item.MarkType != 0 ? item.MarkType : 9,
                         false,
                         this.chartCanvas2,
                         pointEllipses2);
                            }
                            else
                            {
                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        topM + (10 + item.Db) / 10.0 * py,
        item.MarkType != 0 ? item.MarkType : 2,
        false,
        this.chartCanvas2,
        pointEllipses2);

                            }


                            if (lastitem != null)
                            {
                                Line line = new Line();
                                line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

                                line.Y1 = topM + (10 + lastitem.Db) / 10.0 * py;
                                line.X1 = yuandian.X + hzsPxValue[lastitem.Hz] * px;
                                line.Y2 = topM + (10 + item.Db) / 10.0 * py;
                                line.X2 = yuandian.X + hzsPxValue[(item.Hz)] * px;
                                line.StrokeThickness = 1;


                                this.chartCanvas2.Children.Add(line);
                                pointEllipses2.Add(line);

                            }

                            lastitem = item;



                        }

                    }
                }
            }


            else if (mmtype == 1)
            {
                if (isRight)
                {
                    foreach (Shape dataEllipse in pointBcEllipses)
                    {
                        this.chartCanvas.Children.Remove(dataEllipse);
                    }

                    if (list == null) return;
                    TestWindowViewModel vm = DataContext as TestWindowViewModel;


                    TestItem lastitem = null;
                    for (int i = 0; i < this.hzsint.Count(); i++)
                    {
                        //(40,280)原点坐标,上面空45，右边空80
                        // this.chartCanvas.Children.Clear();
                        if (list.ContainsKey(this.hzsint[i]))
                        {
                            TestItem item = list[this.hzsint[i]];


                            if (item.Mask == true)
                            {
                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                              topM + (10 + item.Db) / 10.0 * py,
                              item.MarkType != 0 ? item.MarkType : 6,
                              isRight,
                              this.chartCanvas,
                              pointBcEllipses);



                            }
                            else
                            {
                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                   topM + (10 + item.Db) / 10.0 * py,
                                   item.MarkType != 0 ? item.MarkType : 4,
                                   isRight,
                                   this.chartCanvas,
                                   pointBcEllipses);

                            }





                            if (item.IsUserResp != true)
                            {

                                TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                           topM + (10 + item.Db) / 10.0 * py,
                           isRight,
                            this.chartCanvas,
                           pointBcEllipses);

                            }

                            //骨导默认不连线
                            if (lastitem != null && bcneedlink == 1)
                            {


                                Polyline x_scale = new Polyline();
                                x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));
                                x_scale.StrokeDashArray.Add(10);
                                x_scale.StrokeDashArray.Add(20);
                                ;
                                x_scale.StrokeThickness = 0.5;
                                x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[lastitem.Hz] * px, topM + (10 + lastitem.Db) / 10.0 * py));
                                x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[item.Hz] * px, topM + (10 + item.Db) / 10.0 * py));


                                this.chartCanvas.Children.Add(x_scale);
                                pointBcEllipses.Add(x_scale);

                            }


                            lastitem = item;



                        }

                    }

                }
                else
                {
                    foreach (Shape dataEllipse in pointBcEllipses2)
                    {
                        this.chartCanvas2.Children.Remove(dataEllipse);
                    }

                    if (list == null) return;
                    TestWindowViewModel vm = DataContext as TestWindowViewModel;


                    TestItem lastitem = null;
                    for (int i = 0; i < this.hzsint.Count(); i++)
                    {
                        //(40,280)原点坐标,上面空45，右边空80
                        // this.chartCanvas.Children.Clear();
                        if (list.ContainsKey(this.hzsint[i]))
                        {
                            TestItem item = list[this.hzsint[i]];

                            if (item.IsUserResp != true)
                            {
                                TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                             topM + (10 + item.Db) / 10.0 * py,
                             isRight,
                              this.chartCanvas2,
                             pointBcEllipses2);

                            }


                            if (item.Mask == true)
                            {
                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                              topM + (10 + item.Db) / 10.0 * py,
                              item.MarkType != 0 ? item.MarkType : 5,
                              isRight,
                              this.chartCanvas2,
                              pointBcEllipses2);



                            }
                            else
                            {
                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
                                   topM + (10 + item.Db) / 10.0 * py,
                                   item.MarkType != 0 ? item.MarkType : 3,
                                   isRight,
                                   this.chartCanvas2,
                                   pointBcEllipses2);

                            }

                            if (lastitem != null && bcneedlink == 1)
                            {


                                Polyline x_scale = new Polyline();
                                x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
                                x_scale.StrokeDashArray.Add(10);
                                x_scale.StrokeDashArray.Add(20);
                                ;
                                x_scale.StrokeThickness = 0.5;
                                x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[lastitem.Hz] * px, topM + (10 + lastitem.Db) / 10.0 * py));
                                x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[item.Hz] * px, topM + (10 + item.Db) / 10.0 * py));


                                this.chartCanvas2.Children.Add(x_scale);
                                pointBcEllipses2.Add(x_scale);

                            }

                            lastitem = item;



                        }

                    }
                }
            }

            Console.WriteLine("UpdateTestItem  end");


            //);
        }


        //private void UpdateSelectMemberItem()
        //{
        //    ThreadPool.QueueUserWorkItem(delegate
        //    {
        //    SynchronizationContext.SetSynchronizationContext(new
        //        DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
        //    SynchronizationContext.Current.Post(pl =>
        //    {
        //        this.chartCanvas.Children.Clear();
        //        this.chartCanvas2.Children.Clear();
        //        DrawScale(this.chartCanvas, Color.FromRgb(0xd9, 0x4f, 0x54));
        //        DrawScale(this.chartCanvas2, Color.FromRgb(0x55, 0xa4, 0xdd));
        //    }, null);
        //    });

        //}
        //private void UpdateTestItem(Dictionary<int, TestItem> list, bool isRight, int mmtype = 0, int bcneedlink = 0)
        //{

        //    ThreadPool.QueueUserWorkItem(delegate
        //    {
        //        SynchronizationContext.SetSynchronizationContext(new
        //            DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
        //        SynchronizationContext.Current.Post(pl =>
        //        {
        //            if (mmtype == 0)
        //            {
        //                if (isRight)
        //                {
        //                    foreach (Shape dataEllipse in pointEllipses)
        //                    {
        //                        this.chartCanvas.Children.Remove(dataEllipse);
        //                    }

        //                    if (list == null) return;

        //                    TestItem lastitem = null;
        //                    for (int i = 0; i < this.hzsint.Count(); i++)
        //                    {
        //                        //(40,280)原点坐标,上面空45，右边空80
        //                        // this.chartCanvas.Children.Clear();
        //                        if (list.ContainsKey(this.hzsint[i]))
        //                        {
        //                            TestItem item = list[this.hzsint[i]];
        //                            if (item.Mask == true)
        //                            {
        //                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                                    topM + (10 + item.Db) / 10.0 * py,
        //                                    item.MarkType != 0 ? item.MarkType : 10,
        //                                    true,
        //                                    this.chartCanvas,
        //                                    pointEllipses);

        //                            }
        //                            else
        //                            {


        //                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                         topM + (10 + item.Db) / 10.0 * py,
        //                         item.MarkType != 0 ? item.MarkType : 1,
        //                         true,
        //                         this.chartCanvas,
        //                         pointEllipses);
        //                            }



        //                            if (item.IsUserResp != true)
        //                            {
        //                                TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                                   topM + (10 + item.Db) / 10.0 * py,
        //                                   isRight,
        //                                    this.chartCanvas,
        //                                   pointEllipses);

        //                            }


        //                            if (lastitem != null)
        //                            {



        //                                Line line = new Line();
        //                                line.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

        //                                line.Y1 = topM + (10 + lastitem.Db) / 10.0 * py;
        //                                line.X1 = yuandian.X + hzsPxValue[lastitem.Hz] * px;
        //                                line.Y2 = topM + (10 + item.Db) / 10.0 * py;
        //                                line.X2 = yuandian.X + hzsPxValue[item.Hz] * px;
        //                                line.StrokeThickness = 1;


        //                                this.chartCanvas.Children.Add(line);
        //                                pointBcEllipses.Add(line);

        //                            }


        //                            lastitem = item;



        //                        }

        //                    }

        //                }
        //                else
        //                {
        //                    foreach (Shape dataEllipse in pointEllipses2)
        //                    {
        //                        this.chartCanvas2.Children.Remove(dataEllipse);
        //                    }

        //                    if (list == null) return;
        //                    TestItem lastitem = null;
        //                    for (int i = 0; i < this.hzsint.Count(); i++)
        //                    {
        //                        //(40,280)原点坐标,上面空45，右边空80
        //                        // this.chartCanvas.Children.Clear();
        //                        if (list.ContainsKey(this.hzsint[i]))
        //                        {
        //                            TestItem item = list[this.hzsint[i]];

        //                            if (item.IsUserResp != true)
        //                            {
        //                                TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                                   topM + (10 + item.Db) / 10.0 * py,
        //                                   isRight,
        //                                    this.chartCanvas2,
        //                                   pointEllipses2);

        //                            }


        //                            if (item.Mask == true)
        //                            {


        //                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                         topM + (10 + item.Db) / 10.0 * py,
        //                         item.MarkType != 0 ? item.MarkType : 9,
        //                         false,
        //                         this.chartCanvas2,
        //                         pointEllipses2);
        //                            }
        //                            else
        //                            {
        //                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //        topM + (10 + item.Db) / 10.0 * py,
        //        item.MarkType != 0 ? item.MarkType : 2,
        //        false,
        //        this.chartCanvas2,
        //        pointEllipses2);

        //                            }


        //                            if (lastitem != null)
        //                            {
        //                                Line line = new Line();
        //                                line.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));

        //                                line.Y1 = topM + (10 + lastitem.Db) / 10.0 * py;
        //                                line.X1 = yuandian.X + hzsPxValue[lastitem.Hz] * px;
        //                                line.Y2 = topM + (10 + item.Db) / 10.0 * py;
        //                                line.X2 = yuandian.X + hzsPxValue[(item.Hz)] * px;
        //                                line.StrokeThickness = 1;


        //                                this.chartCanvas2.Children.Add(line);
        //                                pointEllipses2.Add(line);

        //                            }

        //                            lastitem = item;



        //                        }

        //                    }
        //                }
        //            }


        //            else if (mmtype == 1)
        //            {
        //                if (isRight)
        //                {
        //                    foreach (Shape dataEllipse in pointBcEllipses)
        //                    {
        //                        this.chartCanvas.Children.Remove(dataEllipse);
        //                    }

        //                    if (list == null) return;
        //                    TestWindowViewModel vm = DataContext as TestWindowViewModel;


        //                    TestItem lastitem = null;
        //                    for (int i = 0; i < this.hzsint.Count(); i++)
        //                    {
        //                        //(40,280)原点坐标,上面空45，右边空80
        //                        // this.chartCanvas.Children.Clear();
        //                        if (list.ContainsKey(this.hzsint[i]))
        //                        {
        //                            TestItem item = list[this.hzsint[i]];


        //                            if (item.Mask == true)
        //                            {
        //                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                              topM + (10 + item.Db) / 10.0 * py,
        //                              item.MarkType != 0 ? item.MarkType : 6,
        //                              isRight,
        //                              this.chartCanvas,
        //                              pointBcEllipses);



        //                            }
        //                            else
        //                            {
        //                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                                   topM + (10 + item.Db) / 10.0 * py,
        //                                   item.MarkType != 0 ? item.MarkType : 4,
        //                                   isRight,
        //                                   this.chartCanvas,
        //                                   pointBcEllipses);

        //                            }





        //                            if (item.IsUserResp != true)
        //                            {

        //                                TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                           topM + (10 + item.Db) / 10.0 * py,
        //                           isRight,
        //                            this.chartCanvas,
        //                           pointBcEllipses);

        //                            }

        //                            //骨导默认不连线
        //                            if (lastitem != null && bcneedlink == 1)
        //                            {


        //                                Polyline x_scale = new Polyline();
        //                                x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));
        //                                x_scale.StrokeDashArray.Add(10);
        //                                x_scale.StrokeDashArray.Add(20);
        //                                ;
        //                                x_scale.StrokeThickness = 0.5;
        //                                x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[lastitem.Hz] * px, topM + (10 + lastitem.Db) / 10.0 * py));
        //                                x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[item.Hz] * px, topM + (10 + item.Db) / 10.0 * py));


        //                                this.chartCanvas.Children.Add(x_scale);
        //                                pointBcEllipses.Add(x_scale);

        //                            }


        //                            lastitem = item;



        //                        }

        //                    }

        //                }
        //                else
        //                {
        //                    foreach (Shape dataEllipse in pointBcEllipses2)
        //                    {
        //                        this.chartCanvas2.Children.Remove(dataEllipse);
        //                    }

        //                    if (list == null) return;
        //                    TestWindowViewModel vm = DataContext as TestWindowViewModel;


        //                    TestItem lastitem = null;
        //                    for (int i = 0; i < this.hzsint.Count(); i++)
        //                    {
        //                        //(40,280)原点坐标,上面空45，右边空80
        //                        // this.chartCanvas.Children.Clear();
        //                        if (list.ContainsKey(this.hzsint[i]))
        //                        {
        //                            TestItem item = list[this.hzsint[i]];

        //                            if (item.IsUserResp != true)
        //                            {
        //                                TLBIcon.DrawNoRespIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                             topM + (10 + item.Db) / 10.0 * py,
        //                             isRight,
        //                              this.chartCanvas2,
        //                             pointBcEllipses2);

        //                            }


        //                            if (item.Mask == true)
        //                            {
        //                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                              topM + (10 + item.Db) / 10.0 * py,
        //                              item.MarkType != 0 ? item.MarkType : 5,
        //                              isRight,
        //                              this.chartCanvas2,
        //                              pointBcEllipses2);



        //                            }
        //                            else
        //                            {
        //                                TLBIcon.DrawIconByType(yuandian.X + hzsPxValue[item.Hz] * px,
        //                                   topM + (10 + item.Db) / 10.0 * py,
        //                                   item.MarkType != 0 ? item.MarkType : 3,
        //                                   isRight,
        //                                   this.chartCanvas2,
        //                                   pointBcEllipses2);

        //                            }

        //                            if (lastitem != null && bcneedlink == 1)
        //                            {


        //                                Polyline x_scale = new Polyline();
        //                                x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
        //                                x_scale.StrokeDashArray.Add(10);
        //                                x_scale.StrokeDashArray.Add(20);
        //                                ;
        //                                x_scale.StrokeThickness = 0.5;
        //                                x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[lastitem.Hz] * px, topM + (10 + lastitem.Db) / 10.0 * py));
        //                                x_scale.Points.Add(new Point(yuandian.X + hzsPxValue[item.Hz] * px, topM + (10 + item.Db) / 10.0 * py));


        //                                this.chartCanvas2.Children.Add(x_scale);
        //                                pointBcEllipses2.Add(x_scale);

        //                            }

        //                            lastitem = item;



        //                        }

        //                    }
        //                }
        //            }

        //        }, null);
        //    });
        //}

        /// <summary>
        /// 作出箭头
        /// </summary>
        private void DrawArrow()
        {
            Path x_axisArrow = new Path();//x轴箭头
            Path y_axisArrow = new Path();//y轴箭头

            x_axisArrow.Fill = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));
            y_axisArrow.Fill = new SolidColorBrush(Color.FromRgb(0xff, 0, 0));

            PathFigure x_axisFigure = new PathFigure();
            x_axisFigure.IsClosed = true;
            x_axisFigure.StartPoint = new Point(480, 276);                          //路径的起点
            x_axisFigure.Segments.Add(new LineSegment(new Point(480, 284), false)); //第2个点
            x_axisFigure.Segments.Add(new LineSegment(new Point(490, 280), false)); //第3个点

            PathFigure y_axisFigure = new PathFigure();
            y_axisFigure.IsClosed = true;
            y_axisFigure.StartPoint = new Point(36, 30);                          //路径的起点
            y_axisFigure.Segments.Add(new LineSegment(new Point(44, 30), false)); //第2个点
            y_axisFigure.Segments.Add(new LineSegment(new Point(40, 20), false)); //第3个点

            PathGeometry x_axisGeometry = new PathGeometry();
            PathGeometry y_axisGeometry = new PathGeometry();

            x_axisGeometry.Figures.Add(x_axisFigure);
            y_axisGeometry.Figures.Add(y_axisFigure);

            x_axisArrow.Data = x_axisGeometry;
            y_axisArrow.Data = y_axisGeometry;

            this.chartCanvas.Children.Add(x_axisArrow);
            this.chartCanvas.Children.Add(y_axisArrow);
        }
        /// <summary>
        /// 作出x轴和y轴的标尺
        /// </summary>
        private void DrawScale(Canvas canvas, Color color)
        {

            //foreach (int hz in hzsint)
            //{
            //    int value = limitHz[hz];

            //    Rectangle rect = new Rectangle();
            //    //(40 + px * hzsint.IndexOf(hz), 280, px, py * (120 - value));
            //    rect.Fill = new SolidColorBrush(color);

            //    double index = hzsPxValue[hz];


            //    rect.Height = py * (120 - value) / 10.0;

            //    if (index == 0)
            //    {
            //        rect.Width = px / 2;
            //        Canvas.SetLeft(rect, yuandian.X);
            //    }
            //    else if (index ==6)
            //    {
            //        rect.Width = px / 2;
            //        Canvas.SetLeft(rect, yuandian.X + px * index - px / 2);
            //    }
            //    else
            //    {
            //        rect.Width = px;
            //        Canvas.SetLeft(rect, yuandian.X + px * index - px / 2);
            //    }

            //    Canvas.SetTop(rect, yuandian.Y - rect.Height);
            //    canvas.Children.Add(rect);

            //}


            for (int i = 0; i < 4; i += 1)//作480个刻度，因为当前x轴长 480px，每10px作一个小刻度，还预留了一些小空间
            {
                //原点 O=(40,280),s

                Polyline x_scale = new Polyline();
                x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                x_scale.StrokeDashArray.Add(10);
                x_scale.StrokeDashArray.Add(20);
                /// Line x_scale = new Line();
                //x_scale.StrokeEndLineCap = PenLineCap.Triangle;
                //x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                x_scale.StrokeThickness = 0.1;
                x_scale.Points.Add(new Point(yuandian.X + (2.5 + i) * (canvas.Width - yuandian.X - rightM) / 6.0, yuandian.Y));
                x_scale.Points.Add(new Point(yuandian.X + (2.5 + i) * (canvas.Width - yuandian.X - rightM) / 6.0, topM));    //在x轴上的刻度线，起点和终点相同

                //x_scale.Y1 = yuandian.Y;           //与原点坐标的y=280，相同 
                //x_scale.Y2 = topM;


                canvas.Children.Add(x_scale);

            }
            //(40,280)原点坐标,上面空45，右边空80

            //"1500", "3000", "6000",

            for (int i = 0; i < 7; i += 1)//作480个刻度，因为当前x轴长 480px，每10px作一个小刻度，还预留了一些小空间
            {
                //原点 O=(40,280),s
                Line x_scale = new Line();
                x_scale.StrokeEndLineCap = PenLineCap.Triangle;
                x_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                x_scale.StrokeThickness = 0.1;
                x_scale.X1 = yuandian.X + i * (canvas.Width - yuandian.X - rightM) / 6.0;
                x_scale.X2 = x_scale.X1;    //在x轴上的刻度线，起点和终点相同

                x_scale.Y1 = yuandian.Y;           //与原点坐标的y=280，相同 
                x_scale.Y2 = topM;


                canvas.Children.Add(x_scale);


                {
                    TextBlock x_ScaleLabel = new TextBlock();

                    x_ScaleLabel.TextAlignment = TextAlignment.Left;
                    x_ScaleLabel.Width = (canvas.Width - yuandian.X - rightM) / 6.0;
                    x_ScaleLabel.FontSize = 12;
                    x_ScaleLabel.Text = hzs[i]; ;//只给大刻度添加标签，每50px添加一个标签

                    Canvas.SetLeft(x_ScaleLabel, yuandian.X + i * (canvas.Width - yuandian.X - rightM) / 6.0 - 12);//40是原点的坐标，-12是为了让标签看的位置剧中一点
                    Canvas.SetTop(x_ScaleLabel, yuandian.Y + 10);//让标签字往下移一点

                    canvas.Children.Add(x_ScaleLabel);

                }


            }

            for (int i = 0; i < 14; i += 1)//由于y轴短一些，所以在此作出判断，只作25个刻度
            {
                //作出Y轴的刻度
                Line y_scale = new Line();
                y_scale.StrokeEndLineCap = PenLineCap.Triangle;
                y_scale.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));

                y_scale.X1 = yuandian.X;            //原点x=40，在y轴上的刻度线的起点与原点相同
                y_scale.StrokeThickness = 0.1;

                y_scale.X2 = canvas.Width - rightM;
                y_scale.Y1 = topM + i * ((yuandian.Y - topM) / 13.0);  //每10px作一个刻度 
                y_scale.Y2 = y_scale.Y1;    //起点和终点y坐标相同 
                canvas.Children.Add(y_scale);

                if (i % 2 == 1)
                {
                    TextBlock y_ScaleLabel = new TextBlock();
                    y_ScaleLabel.TextAlignment = TextAlignment.Center;
                    y_ScaleLabel.Width = 30;
                    y_ScaleLabel.FontSize = 12;
                    y_ScaleLabel.Text = (-10 + i * 10).ToString();
                    Canvas.SetLeft(y_ScaleLabel, yuandian.X - 30);              //-25px是字体大小的偏移
                    Canvas.SetTop(y_ScaleLabel, topM + i * ((yuandian.Y - topM) / 13.0) - 10);  //280px是原点的坐标，同样-6是为了让标签不要上坐标轴叠上

                    canvas.Children.Add(y_ScaleLabel);
                }

            }
            {
                TextBlock x_ScaleLabel = new TextBlock();
                x_ScaleLabel.Text = "Hz"; ;//只给大刻度添加标签，每50px添加一个标签

                Canvas.SetLeft(x_ScaleLabel, canvas.Width - rightM + 12);//40是原点的坐标，-12是为了让标签看的位置剧中一点
                Canvas.SetTop(x_ScaleLabel, yuandian.Y + 10);//让标签字往下移一点

                canvas.Children.Add(x_ScaleLabel);

                TextBlock y_ScaleLabel = new TextBlock();

                y_ScaleLabel.Text = "dBHL";
                Canvas.SetLeft(y_ScaleLabel, yuandian.X - 30);
                Canvas.SetTop(y_ScaleLabel, topM - 30);

                canvas.Children.Add(y_ScaleLabel);
            }

        }
        /// <summary>
        /// 添加刻度标签
        /// </summary>
        //private void DrawScaleLabel(Canvas canvas)
        //{
        //    for (int i = 1; i < 7; i++)//7 个标签，一共
        //    {
        //        TextBlock x_ScaleLabel = new TextBlock();
        //        TextBlock y_ScaleLabel = new TextBlock();

        //        x_ScaleLabel.Text = (i * 50).ToString();//只给大刻度添加标签，每50px添加一个标签

        //        Canvas.SetLeft(x_ScaleLabel, 40 + 5 * 10 * i - 12);//40是原点的坐标，-12是为了让标签看的位置剧中一点
        //        Canvas.SetTop(x_ScaleLabel, 280 + 2);//让标签字往下移一点

        //        y_ScaleLabel.Text = (i * 50).ToString();
        //        Canvas.SetLeft(y_ScaleLabel, 40 - 25);              //-25px是字体大小的偏移
        //        Canvas.SetTop(y_ScaleLabel, 280 - 5 * 10 * i - 6);  //280px是原点的坐标，同样-6是为了让标签不要上坐标轴叠上

        //        canvas.Children.Add(x_ScaleLabel);
        //        canvas.Children.Add(y_ScaleLabel);
        //    }
        //}

        private void DrawPoint(Canvas canvas, List<Shape> list)
        {
            //随机生成8个点


            //Random rPoint = new Random();
            //for (int i = 0; i < 7; i++)
            //{
            //    int x_point = i * 60;
            //    int y_point = rPoint.Next(250);
            //    dataPoints.Add(new Point(x_point, y_point));
            //}

            //for (int i = 0; i < dataPoints.Count; i++)
            //{
            //    Ellipse dataEllipse = new Ellipse();
            //    dataEllipse.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
            //    dataEllipse.Width = 8;
            //    dataEllipse.Height = 8;

            //    list.Add(dataEllipse);

            //    Canvas.SetLeft(list[i], 40 + dataPoints[i].X - 4);//-4是为了补偿圆点的大小，到精确的位置
            //    Canvas.SetTop(list[i], 280 - dataPoints[i].Y - 4);
            //    //将数据点在画布中的位置保存下来
            //    coordinatePoints.Add(new Point(40 + dataPoints[i].X, 280 - dataPoints[i].Y));
            //    canvas.Children.Add(list[i]);
            //}
        }

        private void DrawCurve(Canvas canvas)
        {
            curvePolyline.Points = coordinatePoints;
            canvas.Children.Add(curvePolyline);
        }
        private void AddCurvePoint(Point dataPoint)
        {

            dataPoints.Add(dataPoint);
            Ellipse dataEllipse = new Ellipse();
            dataEllipse.Fill = new SolidColorBrush(Color.FromRgb(0, 0, 0xff));
            dataEllipse.Width = 8;
            dataEllipse.Height = 8;

            Random rPoint = new Random();
            //    Canvas.SetTop(list[i], 280 - dataPoints[i].Y - 4);
            //    //将数据点在画布中的位置保存下来
            coordinatePoints.Add(new Point(50 * rPoint.Next(7), dataPoint.Y));
            this.chartCanvas.Children.Add(dataEllipse);         //  int i = dataPoints.Count;

            //Random rPoint = new Random();
            // int x_point = i * 60;
            // int y_point = rPoint.Next(250);
            //  dataPoints.Add(new Point(x_point, y_point));
            // for (int i = 0; i < dataPoints.Count; i++)
            //{
            //每一个点的X数据都要向左移动50px
            //    dataPoints[i] = new Point(dataPoints[i].X - 50, dataPoints[i].Y);
            //    coordinatePoints[i] = new Point(40 + dataPoints[i].X, 280 - dataPoints[i].Y);

            // Canvas.SetLeft(pointEllipses[i], 40 + dataPoints[i].X - 4);//-4是为了补偿圆点的大小，到精确的位置
            //  Canvas.SetTop(pointEllipses[i], 280 - dataPoints[i].Y - 4);
            // }
        }

        private void chartCanvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //随机生成Y坐标
            //Point dataPoint = new Point((new Random()).Next(250), (new Random()).Next(250));

            //AddCurvePoint(dataPoint);
        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;

            var c = button.DataContext as PureToneResult;

            var model = DataContext as UserPageViewModel;
            new TestWindow(model.SelectMember).ShowDialog();
            // MessageBox.Show(c.Uid);

        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            // new TestWindow().ShowDialog();
            var button = sender as Button;

            var c = button.DataContext as PureToneResult;

            var model = DataContext as UserPageViewModel;
            //MessageBox.Show(c.Uid);
            DMSkin.Core.Broadcast.PushBroadcast("Navigation", new PreviewPage(c, model.SelectMember));
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;

            var c = button.DataContext as PureToneResult;

            // MessageBox.Show(c.Uid);

            var result = MessageBoxX.Show("确认删除这条测试记录？", "提醒", Application.Current.MainWindow, MessageBoxButton.YesNo, new MessageBoxXConfigurations()
            {
                MessageBoxStyle = MessageBoxStyle.Modern,
                MessageBoxIcon = MessageBoxIcon.Warning,
                ButtonBrush = "#F1C825".ToColor().ToBrush(),
            });

            if (result == MessageBoxResult.Yes)
            {
                var model = DataContext as UserPageViewModel;
                model.deleteTestResultByUid(c.Uid);
                // DialogService.ShowMessage("Delete friend successfully");
            }
        }


        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            try
            {
                //言语
                var model = DataContext as UserPageViewModel;
                new SpeechWindow(model.SelectMember).ShowDialog();
            }
            catch
            {
                var result = MessageBoxX.Show("言语测试遇到异常错误，请重试", "提醒", Application.Current.MainWindow, MessageBoxButton.YesNo, new MessageBoxXConfigurations()
                {
                    MessageBoxStyle = MessageBoxStyle.Modern,
                    MessageBoxIcon = MessageBoxIcon.Warning,
                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                });

                if (result == MessageBoxResult.Yes)
                {

                }
            }


        }


        private void Button_Click_11(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;

            //var c = button.DataContext as PureToneResult;

            var model = DataContext as UserPageViewModel;
            new SpeechWindow(model.SelectMember).ShowDialog();
            //// MessageBox.Show(c.Uid);

        }
        private void Button_Click_22(object sender, RoutedEventArgs e)
        {
            // new TestWindow().ShowDialog();
            var button = sender as Button;

            var c = button.DataContext as SpeechResultItem;

            var model = DataContext as UserPageViewModel;
            ////MessageBox.Show(c.Uid);
            DMSkin.Core.Broadcast.PushBroadcast("Navigation", new PreviewSpeechPage(c, model.SelectMember));
        }

        private void Button_Click_33(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;

            var c = button.DataContext as SpeechResultItem;

            // MessageBox.Show(c.Uid);

            var result = MessageBoxX.Show("确认删除这条测试记录？", "提醒", Application.Current.MainWindow, MessageBoxButton.YesNo, new MessageBoxXConfigurations()
            {
                MessageBoxStyle = MessageBoxStyle.Modern,
                MessageBoxIcon = MessageBoxIcon.Warning,
                ButtonBrush = "#F1C825".ToColor().ToBrush(),
            });

            if (result == MessageBoxResult.Yes)
            {
                var model = DataContext as UserPageViewModel;
                model.deleteSpeechTestResult(c);
                // DialogService.ShowMessage("Delete friend successfully");
            }
        }

        private void instScroll_Loaded(object sender, RoutedEventArgs e)
        {
            ResultsListView.AddHandler(MouseWheelEvent, new RoutedEventHandler(MyMouseWheelH), true);
        }

        private void MyMouseWheelH(object sender, RoutedEventArgs e)
        {

            MouseWheelEventArgs eargs = (MouseWheelEventArgs)e;

            double x = (double)eargs.Delta;

            double y = instScroll.VerticalOffset;

            instScroll.ScrollToVerticalOffset(y - x);
        }
    }
}
